#include "cl_precompiled.h"
#include "gui_local.h"

template<> GuiSystem *Ogre::Singleton<GuiSystem>::ms_Singleton=0;
GuiSystem gui_system;

static void onAcceptTest(){
	new GuiMessageBox("Another box","test");
}
static bool onTestBtn_clicked(const CEGUI::EventArgs &e){
	static GuiMessageBox *msgbox;
	if(msgbox)delete msgbox;
	msgbox=new GuiMessageBox("This is a test message box. When you press ok it will generate a signal onAccept which can be bound to any function","Test box");
	msgbox->onAccept.connect(boost::bind(onAcceptTest));
	return true;
}

GuiSystem::GuiSystem(){
	mRunning=false;
	mGuiSystem=0;
	mRenderer=0;
}
GuiSystem::~GuiSystem(){
	shutdown();
}
void GuiSystem::init(Ogre::RenderWindow *window, Ogre::SceneManager *scene){
	if(mRunning)return;
	//setup cegui
	try{
		using namespace CEGUI;
		mRenderer=new CEGUI::OgreCEGUIRenderer(window,Ogre::RENDER_QUEUE_OVERLAY, false, 3000, scene);
		mGuiSystem=new System(mRenderer);
		mGuiSystem->setScriptingModule(new LuaScriptModule());

		//load main imagesets that are needed for gui layouts
		ImagesetManager::getSingleton().createImageset("gui\\imagesets\\Quantum.imageset");
		ImagesetManager::getSingleton().createImageset("gui\\imagesets\\Vanilla.imageset");
		ImagesetManager::getSingleton().createImagesetFromImageFile("Main/Background", "images/background.tga");
		
		//load fonts
		FontManager::getSingleton().createFont("gui\\fonts\\serpentine-12.font");
		//load themes
		WidgetLookManager::getSingleton().parseLookNFeelSpecification("gui\\looknfeel\\Quantum.looknfeel");
		WidgetLookManager::getSingleton().parseLookNFeelSpecification("gui\\looknfeel\\Vanilla.looknfeel");
		//load bindings
		SchemeManager::getSingleton().loadScheme("gui\\schemes\\Quantum.scheme");
		SchemeManager::getSingleton().loadScheme("gui\\schemes\\Vanilla.scheme");
		
		//set default cursor
		System::getSingleton().setDefaultMouseCursor(
			&ImagesetManager::getSingleton().getImageset("QuantumImages")->getImage("MouseArrow"));
		
		//execute startup script
		System::getSingleton().executeScriptFile("init.lua");

		WindowManager &wm=WindowManager::getSingleton();
		GUISheet *main=new GUISheet("DefaultWindow","MainMenu");
		mGuiSystem->setGUISheet(main);
		Window *bg=wm.createWindow("Quantum/StaticImage","MainBG");
		bg->setSize(UVector2(cegui_reldim(1),cegui_reldim(1)));
		bg->setProperty("Image", "set:Main/Background image:full_image");

		//mConsole=new GuiConsole();

		Window *btn=wm.createWindow("Quantum/Button","Main/Connect");
		btn->setSize(UVector2(cegui_reldim(0.33),cegui_reldim(0.1)));
		btn->setPosition(UVector2(cegui_reldim(0.33),cegui_reldim(0.2)));
		btn->setText("Test");
		btn->subscribeEvent(PushButton::EventClicked,
			Event::Subscriber(&onTestBtn_clicked));
		bg->addChildWindow(btn);
		main->addChildWindow(bg);
	}catch(CEGUI::GenericException &e){
		if(mGuiSystem)delete mGuiSystem;
		if(mRenderer)delete mRenderer;
		throw Ogre::Exception(0,e.getMessage().c_str(),"GuiSystem::init()");
	}
	mRunning=true;
}
void GuiSystem::shutdown(){
	if(!mRunning)return;
	CEGUI::ScriptModule *m=mGuiSystem->getScriptingModule();
	delete mConsole;
	delete mGuiSystem;
	delete m;
	delete mRenderer;
	mRunning=false;
}
void GuiSystem::update(){
	CEGUI::System::getSingleton().renderGUI();
}
void GuiSystem::onMouseMove(int relx, int rely){
	CEGUI::System::getSingleton().injectMouseMove( relx,rely );
}
void GuiSystem::onMouseDown(int x, int y, int button){
	CEGUI::System::getSingleton().injectMouseButtonDown((CEGUI::MouseButton)button);
}
void GuiSystem::onMouseUp(int x, int y, int button){
	CEGUI::System::getSingleton().injectMouseButtonUp((CEGUI::MouseButton)button);
}
void GuiSystem::onKeyDown(int keycode, int text){
	CEGUI::System::getSingleton().injectKeyDown(keycode);
	CEGUI::System::getSingleton().injectChar(text);
}
void GuiSystem::onKeyUp(int keycode, int text){
	CEGUI::System::getSingleton().injectKeyUp(keycode);
}

Gui &Gui::getSingleton(){
	return GuiSystem::getSingleton();
}
Gui *Gui::getSingletonPtr(){
	return GuiSystem::getSingletonPtr();
}